package com.teragadgets.android.gameboy;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class Settings extends Activity {

	private Button aButton, bButton, upButton, downButton, leftButton,
			rightButton, startButton, selectButton;
	private CheckBox hideTop, fullscreen, touchControls, drawFPS;
	private Spinner thumbnails;

	private SharedPreferences pref;

	private int selectedButton = -1;

	@Override
	/* Initialize JavaBoy when run as an application */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		pref = getSharedPreferences("com.teragadgets.android.gameboy", 0);

		thumbnails = (Spinner) this.findViewById(R.id.thumbnails);
		thumbnails.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				SharedPreferences.Editor editor = pref.edit();
				editor.putInt("thumbnails", position);
				editor.commit();
				KiddGBC.thumbs.clear();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});

		fullscreen = (CheckBox) this.findViewById(R.id.fullscreen);
		fullscreen.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				SharedPreferences.Editor editor = pref.edit();
				if (isChecked)
					editor.putBoolean("fullscreen", true);
				else
					editor.putBoolean("fullscreen", false);
				editor.commit();
			}
		});

		hideTop = (CheckBox) this.findViewById(R.id.hidetop);
		hideTop.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				SharedPreferences.Editor editor = pref.edit();
				if (isChecked)
					editor.putBoolean("hidetop", true);
				else
					editor.putBoolean("hidetop", false);
				editor.commit();
			}
		});

		drawFPS = (CheckBox) this.findViewById(R.id.drawfps);
		drawFPS.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				SharedPreferences.Editor editor = pref.edit();
				if (isChecked)
					editor.putBoolean("drawFPS", true);
				else
					editor.putBoolean("drawFPS", false);
				editor.commit();
			}
		});

		aButton = (Button) this.findViewById(R.id.a);
		aButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				selectedButton = 0;
				Toast.makeText(Settings.this, "Press new key for A button now",
						Toast.LENGTH_SHORT).show();
			}
		});
		bButton = (Button) this.findViewById(R.id.b);
		bButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				selectedButton = 1;
				Toast.makeText(Settings.this, "Press new key for B button now",
						Toast.LENGTH_SHORT).show();
			}
		});
		upButton = (Button) this.findViewById(R.id.up);
		upButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				selectedButton = 2;
				Toast.makeText(Settings.this,
						"Press new key for Up button now", Toast.LENGTH_SHORT)
						.show();
			}
		});
		downButton = (Button) this.findViewById(R.id.down);
		downButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				selectedButton = 3;
				Toast
						.makeText(Settings.this,
								"Press new key for Down button now",
								Toast.LENGTH_SHORT).show();
			}
		});
		leftButton = (Button) this.findViewById(R.id.left);
		leftButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				selectedButton = 4;
				Toast
						.makeText(Settings.this,
								"Press new key for Left button now",
								Toast.LENGTH_SHORT).show();
			}
		});
		rightButton = (Button) this.findViewById(R.id.right);
		rightButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				selectedButton = 5;
				Toast.makeText(Settings.this,
						"Press new key for Right button now",
						Toast.LENGTH_SHORT).show();
			}
		});
		startButton = (Button) this.findViewById(R.id.start);
		startButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				selectedButton = 6;
				Toast.makeText(Settings.this,
						"Press new key for Start button now",
						Toast.LENGTH_SHORT).show();
			}
		});
		selectButton = (Button) this.findViewById(R.id.select);
		selectButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				selectedButton = 7;
				Toast.makeText(Settings.this,
						"Press new key for Select Button now",
						Toast.LENGTH_SHORT).show();
			}
		});

		touchControls = (CheckBox) this.findViewById(R.id.touch);
		touchControls.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				SharedPreferences.Editor editor = pref.edit();
				if (isChecked) {
					editor.putBoolean("touch", true);
					aButton.setEnabled(false);
					bButton.setEnabled(false);
					upButton.setEnabled(false);
					downButton.setEnabled(false);
					leftButton.setEnabled(false);
					rightButton.setEnabled(false);
					startButton.setEnabled(false);
					selectButton.setEnabled(false);
				} else {
					editor.putBoolean("touch", false);
					aButton.setEnabled(true);
					bButton.setEnabled(true);
					upButton.setEnabled(true);
					downButton.setEnabled(true);
					leftButton.setEnabled(true);
					rightButton.setEnabled(true);
					startButton.setEnabled(true);
					selectButton.setEnabled(true);
				}
				editor.commit();
			}
		});

		thumbnails.setSelection(pref.getInt("thumbnails", 1));
		hideTop.setChecked(pref.getBoolean("hidetop", false));
		fullscreen.setChecked(pref.getBoolean("fullscreen", false));
		drawFPS.setChecked(pref.getBoolean("drawFPS", true));
		touchControls.setChecked(pref.getBoolean("touch", false));
		aButton.setText(getKeyName(pref.getInt("aButton", KeyEvent.KEYCODE_M)));
		bButton.setText(getKeyName(pref.getInt("bButton", KeyEvent.KEYCODE_N)));
		upButton
				.setText(getKeyName(pref.getInt("upButton", KeyEvent.KEYCODE_W)));
		downButton.setText(getKeyName(pref.getInt("downButton",
				KeyEvent.KEYCODE_S)));
		leftButton.setText(getKeyName(pref.getInt("leftButton",
				KeyEvent.KEYCODE_A)));
		rightButton.setText(getKeyName(pref.getInt("rightButton",
				KeyEvent.KEYCODE_D)));
		startButton.setText(getKeyName(pref.getInt("startButton",
				KeyEvent.KEYCODE_ENTER)));
		selectButton.setText(getKeyName(pref.getInt("selectButton",
				KeyEvent.KEYCODE_DEL)));
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		super.onKeyDown(keyCode, event);
		if (keyCode == KeyEvent.KEYCODE_BACK)
			return false;
		SharedPreferences.Editor editor = pref.edit();
		switch (selectedButton) {
		case 0:
			System.out.println(keyCode + " "
					+ pref.getInt("aButton", KeyEvent.KEYCODE_A));
			editor.putInt("aButton", keyCode);
			aButton.setText(getKeyName(keyCode));
			break;
		case 1:
			bButton.setText(getKeyName(keyCode));
			editor.putInt("bButton", keyCode);
			break;
		case 2:
			upButton.setText(getKeyName(keyCode));
			editor.putInt("upButton", keyCode);
			break;
		case 3:
			downButton.setText(getKeyName(keyCode));
			editor.putInt("downButton", keyCode);
			break;
		case 4:
			leftButton.setText(getKeyName(keyCode));
			editor.putInt("leftButton", keyCode);
			break;
		case 5:
			rightButton.setText(getKeyName(keyCode));
			editor.putInt("rightButton", keyCode);
			break;
		case 6:
			startButton.setText(getKeyName(keyCode));
			editor.putInt("startButton", keyCode);
			break;
		case 7:
			selectButton.setText(getKeyName(keyCode));
			editor.putInt("selectButton", keyCode);
			break;
		}
		editor.commit();
		selectedButton = -1;
		return false;
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		super.onKeyUp(keyCode, event);
		return false;
	}

	public static String getKeyName(int code) {
		if (code == KeyEvent.KEYCODE_ENTER)
			return "Enter";
		else if (code == KeyEvent.KEYCODE_DEL)
			return "Del";
		KeyEvent key = new KeyEvent(KeyEvent.ACTION_DOWN, code);
		return Character.toString(key.getDisplayLabel());
	}

}
